package com.wanhe.mealbookmodule.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;
import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.disgn.ViewItem;
import com.wanhe.base.model.holder.DishHolder;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.mealbookmodule.R;
import com.wanhe.viewconf.ViewCreator;
import com.wanhe.viewconf.adapter.BaseMenuPageItemAdapter;
import com.wanhe.viewconf.generator.IViewGenerator;
import com.wanhe.viewconf.views.child.BaseXYView;
import com.wanhe.viewconf.views.child.MealBookChildItemView;

import java.util.List;

/**
 * File Name:MealBookItemsAdapter.java<br/>
 * Package Name:com.wanhe.mealbookmodule.adapter<br/>
 * Date:2017/7/14 11:08<br/>
 * author:chendeji<br/>
 * desc: 坐标布局的控件适配器<br/>
 * Copyright (c) 2017/7/14, 781571323@qq.com All Rights Reserved.
 *
 */
public class MealBookItemsAdapter extends BaseMenuPageItemAdapter<ViewItem> {
	public static final String	TAG	= "MealBookItemsAdapter";

	private int					mPageIndex;

	public MealBookItemsAdapter(Context context, List<ViewItem> list) {
		super(context, list);
	}

	@Override
	public String getItemType(int position) {
		ViewItem model = (ViewItem) getItem(position);
		return model.getType();
	}

	@Override
	public BaseXYView getXYView(int position, View convertView, ViewGroup parent) {
		String itemType = getItemType(position);
		ViewItem model = (ViewItem) getItem(position);
		Dish dish = DishHolder.getInstance().get(model.getId());
		if (ObjectUtil.checkObject(dish)){
			dish.tid = model.getTid();
		}
		IViewGenerator viewGenerator = ViewCreator.getInstance().createXYView(itemType);
		if (!ObjectUtil.checkObject(viewGenerator)) {
			Logger.t(TAG).e("type missing  %s", itemType);
			return null;
		}
		BaseXYView view = viewGenerator.createView(this.mContext);
		view.setLeftPT(model.getViewport() == null ? 0 : model.getViewport().getLeft());
		view.setTopPT(model.getViewport() == null ? 0 : model.getViewport().getTop());
		view.setWidthPT(model.getViewport() == null ? 0 : model.getViewport().getWidth());
		view.setHeightPT(model.getViewport() == null ? 0 : model.getViewport().getHeight());

		if (view instanceof MealBookChildItemView) {
			MealBookChildItemView itemView = (MealBookChildItemView) view;
			itemView.bindData(dish);
			itemView.initModelData(model);
			itemView.setViewColor(
					model.getViewport() == null ? "" : model.getViewport().getColor());
			itemView.setPagePosition(mPageIndex);
		}

		// if (BuildConfig.DEBUG) {
		if (ViewCreator.getInstance().isDebug()) {
			view.setBackgroundDrawable(
					mContext.getResources().getDrawable(R.drawable.xyscale_view_bg));
		}
		// }

		return view;
	}

	public void setPageIndex(int mPageIndex) {
		this.mPageIndex = mPageIndex;
	}
}
